package com.yjz.media.study.jni;

import com.blankj.utilcode.util.FileIOUtils;
import com.yjz.media.study.MediaApplication;
import com.yjz.media.study.util.ByteUtils;

/**
 * 作用 : x264编码
 * 作者 : yjz
 * 时间 : 2023/11/8 16:50
 */
public class X264NativeHelper {

    static {
        System.loadLibrary("study");
    }

    public volatile static X264NativeHelper x264NativeHelper;

    public static X264NativeHelper getInstance() {
        if (x264NativeHelper == null) {
            synchronized (X264NativeHelper.class) {
                if (x264NativeHelper == null) {
                    x264NativeHelper = new X264NativeHelper();
                }
            }
        }
        return x264NativeHelper;
    }

    public native boolean init(int width, int height, int fps, int bitrate);

    /**
     * JNI层调用Java层的回调方法
     *
     * @param data 使用x264软编码的数据
     */
    private void postData(byte[] data) {
        // h264文件
        FileIOUtils.writeFileFromBytesByStream(MediaApplication.getContext().getExternalCacheDir() + "/x264_enc.h264", data, true);
        String hexBytesToString = ByteUtils.hexBytesToString(data);
        // h264数据
        FileIOUtils.writeFileFromString(MediaApplication.getContext().getExternalCacheDir() + "/x264_enc.txt", hexBytesToString, true);
    }

    public native void encodeData(byte[] buffer, int length, int type);
}
